package com.youlai.boot.modules.notice.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.modules.notice.mapper.BfNoticeMapper;
import com.youlai.boot.modules.notice.model.BfNotice;
import com.youlai.boot.modules.notice.model.BfNoticeConverter;
import com.youlai.boot.modules.notice.model.BfNoticeForm;
import com.youlai.boot.modules.notice.model.BfNoticePageQuery;
import com.youlai.boot.modules.notice.model.BfNoticeVO;
import com.youlai.boot.system.enums.NoticePublishStatusEnum;
import com.youlai.boot.system.enums.NoticeTargetEnum;
import com.youlai.boot.system.model.entity.Notice;
import com.youlai.boot.system.model.form.NoticeForm;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class BfNoticeService extends ServiceImpl<BfNoticeMapper,BfNotice> {

    private final BfNoticeConverter bfNoticeConverter;
    private final BfNoticeMapper bfNoticeMapper;

    public boolean save(BfNoticeForm form) {
        BfNotice notice = bfNoticeConverter.toEntity(form);
        notice.setCreateBy(SecurityUtils.getUserId());
        
        return this.save(notice);
    }

     /**
     * 更新通知公告
     *
     * @param id       通知公告ID
     * @param formData 通知公告表单对象
     * @return {@link Boolean} 是否更新成功
     */    
    public boolean updateNotice(Long id, BfNoticeForm formData) {        
        BfNotice entity = bfNoticeConverter.toEntity(formData);
        return this.updateById(entity);
    }

    public IPage<BfNoticeVO> getBfNoticePage(BfNoticePageQuery query) {
        Page<BfNoticeVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return bfNoticeMapper.getBfNoticePage(page, query);
    }

    public boolean publishNotice(Long id) {
        BfNotice notice = this.getById(id);
        if (notice == null) {
            throw new BusinessException("通知公告不存在");
        }

        if (NoticePublishStatusEnum.PUBLISHED.getValue().equals(notice.getStatus())) {
            throw new BusinessException("通知公告已发布");
        }

        notice.setStatus(NoticePublishStatusEnum.PUBLISHED.getValue());
        return this.updateById(notice);
    }

    public boolean revokeNotice(Long id) {
        BfNotice notice = this.getById(id);
        if (notice == null) {
            throw new BusinessException("通知公告不存在");
        }

        if (!NoticePublishStatusEnum.PUBLISHED.getValue().equals(notice.getStatus())) {
            throw new BusinessException("通知公告未发布或已撤回");
        }

        notice.setStatus(NoticePublishStatusEnum.REVOKED.getValue());
        return this.updateById(notice);
    }

    public boolean deleteNotices(String ids) {
        if (StrUtil.isBlank(ids)) {
            throw new BusinessException("删除的通知公告数据为空");
        }

        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();

        return this.removeByIds(idList);
    }

     /**
     * 获取通知公告表单数据
     *
     * @param id 通知公告ID
     * @return {@link NoticeForm} 通知公告表单对象
     */    
    public BfNoticeForm getFormData(Long id) {
        BfNotice entity = this.getById(id);
        return bfNoticeConverter.toForm(entity);
    }

    public BfNoticeVO getNoticeDetail(Long id) {
        BfNotice notice = this.getById(id);
        return bfNoticeConverter.toNoticeVO(notice);
    }
}
